﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UIWindows.Domains;
using UIWindows.Enums;
using UIWindows.GlobalStatics;
using UIWindows.Tools;
using UIWindows.Tools.Helper;
using UIWindows.ViewModels;

namespace UIWindows
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel();
            WindowState = WindowState.Maximized;
            ThreadPool.SetMinThreads(64, 64);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var model = ObjectContainer.ResolveSingleton<TemporaryStatic>();
            try
            {
                if (model.IsRunStatus || DialogHost.IsDialogOpen(PopUpTypes.Global.ToString()))
                {
                    e.Cancel = true;
                }
                else if (MessageBox.Show("确定是退出吗？", "询问", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No) { e.Cancel = true; }
            }
            catch (Exception)
            {

            }
            if (!e.Cancel)
            {
                //关闭软件保存产量信息
                ObjectContainer.ResolveSingleton<HomeStatic>().SaveChartData();

                ObjectContainer.ResolveSingleton<HomeStatic>().SaveEquipmentTime();
                KeyboardHook.Stop();
            }
        }

        private void DialogHost_Loaded(object sender, RoutedEventArgs e)
        {
            KeyboardHook.Start();
        }
    }
}
